package elementosJuego;

import java.awt.Rectangle;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import servidor.GestorJuego;

public abstract class Figura{
    protected Integer x, y, w, h;
    private int coord[];
    protected GestorJuego gestorJuego;
    protected char apunta;

    protected Figura(Integer x0,Integer y0,Integer w0,Integer h0,GestorJuego gestorJuego){
        x = x0;
        y = y0;
        h = h0;
        w = w0;
        coord = new int[2];
        this.gestorJuego = gestorJuego;
    }

    public void colisionoBalafalse(Integer i){}
    public void colisionoBalatrue(Integer i){
        try {
            getClass().getMethod("sosvosBala" + Boolean.valueOf(this.equals(gestorJuego.getBalas().get(i))).toString(), Integer.class).invoke(this, i);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void sosvosBalatrue(Integer i){}
    public void sosvosBalafalse(Integer i){
        this.colisionoCon((Bala)gestorJuego.getBalas().get(i));
    }

    public void colisionoJugadorfalse(Integer i){}
    public void colisionoJugadortrue(Integer i){
        try {
            getClass().getMethod("sosvosJugador" + Boolean.valueOf(this.equals(gestorJuego.getJugadores().get(i))).toString(), Integer.class).invoke(this, i);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void sosvosJugadortrue(Integer i){}
    public void sosvosJugadorfalse(Integer i){
        this.colisionoCon((Jugador)gestorJuego.getJugadores().get(i));
    }

    public void colisionoParedfalse(Integer i){}
    public void colisionoParedtrue(Integer i){
        try {
            getClass().getMethod("sosvosPared" + Boolean.valueOf(this.equals(gestorJuego.getParedes().get(i))).toString(), Integer.class).invoke(this, i);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Figura.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void sosvosParedtrue(Integer i){}
    public void sosvosParedfalse(Integer i){
        this.colisionoCon((Pared)gestorJuego.getParedes().get(i));
    }

    public Rectangle rect(){
        return new Rectangle(x, y, w, h);
    }

    public void moverA(){
        coord[0] = x;
        coord[1] = y;
        y-=5;
        apunta = 'A';
        gestorJuego.detectarColision(this);
    }
    public void moverB(){
        coord[0] = x;
        coord[1] = y;
        y+=5;
        apunta = 'B';
        gestorJuego.detectarColision(this);
    }
    public void moverI(){
        coord[0] = x;
        coord[1] = y;
        x -= 5;
        apunta = 'I';
        gestorJuego.detectarColision(this);
    }
    public void moverD(){
        coord[0] = x;
        coord[1] = y;
        x += 5;
        apunta = 'D';
        gestorJuego.detectarColision(this);
    }
    public void returnCoord(){
        x = coord[0];
        y = coord[1];
    }

    public char getApunta(){
        return apunta;
    }

    public int getApuntaInt(){
        return (int)apunta;
    }

    public void setApunta(char apunta){
        this.apunta = apunta;
    }

    public abstract void colisionoCon(Jugador c);
    public abstract void colisionoCon(Bala c);
    public abstract void colisionoCon(Pared c);
   
    public Integer getX() {
        return x;
    }

    public Integer getY() {
        return y;
    }
}